package logic;

import java.io.Serializable;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import bean.User;

import logicInterface.AdminLogicInterface;
import manager.UserManager;
import utils.DBService;
import vo.admin.AddUserPanelVO;
import vo.admin.LoginPanelVO;
import vo.admin.MainPanelVO;
import vo.admin.ModifyUserPanelVO;


public class AdminLogic extends UnicastRemoteObject implements AdminLogicInterface,Serializable{
	private LoginPanelVO lpvo;
	private AddUserPanelVO aupvo;
	private MainPanelVO mpvo;
	private ModifyUserPanelVO mupvo;

	private String state;
	private String message;
	
	private UserManager userManager;
	
	public AdminLogic() throws RemoteException{
		lpvo=new LoginPanelVO();
		aupvo=new AddUserPanelVO();
		mpvo=new MainPanelVO();
		mupvo=new ModifyUserPanelVO();

		state="Login";
		message="";
        
		userManager=UserManager.getInstance();
	
	}

	public LoginPanelVO getLpvo() throws RemoteException{
		return lpvo;
	}

	public void setLpvo(LoginPanelVO lpvo) throws RemoteException{
		this.lpvo = lpvo;
	}

	public AddUserPanelVO getAupvo() throws RemoteException{
		return aupvo;
	}

	public void setAupvo(AddUserPanelVO aupvo) throws RemoteException{
		this.aupvo = aupvo;
	}



	public MainPanelVO getMpvo() throws RemoteException{
		return mpvo;
	}



	public void setMpvo(MainPanelVO mpvo) throws RemoteException{
		this.mpvo = mpvo;
	}



	public ModifyUserPanelVO getMupvo() throws RemoteException{
		return mupvo;
	}



	public void setMupvo(ModifyUserPanelVO mupvo) throws RemoteException{
		this.mupvo = mupvo;
	}



	public String searchUser() throws RemoteException{
		mpvo.setUserData(userManager.getUserData(mpvo.getUserType(),mpvo.getKeywordType(),mpvo.getKeyword()));
		return state;
	}


	public String addUser() throws RemoteException{
		User user=new User();
		user.setUserName(aupvo.getUsername());
		user.setPassword(aupvo.getPassword());
		user.setType(aupvo.getType()+1);
		if(userManager.signUp(user)){
			message="添加成功";
		}else{
			message="用户名已存在";
		}
		return state;
	}

	public String modifyUser() throws RemoteException{
		User user=new User();
		user.setUserName(mpvo.getUserData()[mpvo.getSelectedRow()][0]);
		user.setPassword(mupvo.getPassword());
		userManager.updateUserPassword(user);
		message="修改成功";
		return state;
	}
	
    public String toAddUser() throws RemoteException{
		state="AddUser";
		return state;
	}



	public String toModifyUser() throws RemoteException{
		if(mpvo.getSelectedRow()==-1){
			message="请选中所要修改的行！";
		}else{
			state="ModifyUser";
		}
		return state;
	}


	public String deleteUser() throws RemoteException{
		if(mpvo.getSelectedRow()==-1){
			message="请选中所要删除的行！";
		}else{
			userManager.deleteUser(mpvo.getUserData()[mpvo.getSelectedRow()][0]);
			message="删除成功";
		}
		return state;
	}
	
	public String login() throws RemoteException{
		if(userManager.isAdmin(lpvo.getUsername(),lpvo.getPassword())){
			state="Main";
		}else{
			message="登录失败";
		}
		return state;
	}

	public String logout() throws RemoteException{
		state="Login";
		return state;
	}

	public String toMain() throws RemoteException{
		state="Main";
		return state;
	}


	public String getState() throws RemoteException{
		return state;
	}

	public String getMessage() throws RemoteException{
		return message;
	}
	
	public void setMessage(String s) throws RemoteException{
		message=s;
	}

	public void setState(String s) throws RemoteException{
		state=s;
	}



}
